﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceDog.Lib.Poker
{
    public partial class Card : ICloneable, IComparable<Card>, IComparable
    {
        /// <summary>
        /// 使用王牌花色
        /// </summary>
        public static bool useTrump = false;
        /// <summary>
        /// 王牌花色
        /// </summary>
        public static Suit trump = Suit.Club;
        /// <summary>
        /// 确定A点是比K大还是比2小
        /// </summary>
        public static bool isAceHigh = true;
        /// <summary>
        /// 花色
        /// </summary>
        public Suit Suit { get; private set; }
        /// <summary>
        /// 序号
        /// </summary>
        public Rank Rank { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        private Card() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="suit"></param>
        /// <param name="rank"></param>
        public Card(Suit suit, Rank rank)
        {
            this.Suit = suit;
            this.Rank = rank;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0} {1}", this.Suit, this.Rank);
        }
        public string ToChineseString()
        {
            string[] suitArr = new[] { "梅花", "方块", "红桃", "黑桃" };
            string[] rankArr = new[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
            return string.Format("{0}{1}", suitArr[(int)this.Suit - 1], rankArr[(int)this.Rank - 1]);
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }


        public static bool operator >(Card card1, Card card2)
        {
            if (card1.Suit == card2.Suit)
            {
                if (isAceHigh)
                {
                    if (card1.Rank == Rank.Ace)
                    {
                        if (card2.Rank == Rank.Ace)
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (card2.Rank == Rank.Ace)
                        {
                            return false;
                        }
                        else
                        {
                            return card1.Rank > card2.Rank;
                        }
                    }
                }
                else
                {
                    return card1.Rank > card2.Rank;
                }
            }
            else
            {
                if (useTrump && (card2.Suit == trump))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        public static bool operator <(Card card1, Card card2)
        {
            return !(card1 > card2);
        }
        public static bool operator >=(Card card1, Card card2)
        {
            return (card1 > card2) || (card1 == card2);
        }
        public static bool operator <=(Card card1, Card card2)
        {
            return !(card1 >= card2);
        }
        public static bool operator ==(Card card1, Card card2)
        {
            return (card1.Suit == card2.Suit) && (card1.Rank == card2.Rank);
        }
        public static bool operator !=(Card card1, Card card2)
        {
            return !(card1 == card2);
        }
        public override bool Equals(object card)
        {
            return this == (Card)card;
        }
        public override int GetHashCode()
        {
            return ((int)this.Suit - 1) * 13 + (int)this.Rank;
        }

        public int CompareTo(Card other)
        {
            return this.GetHashCode() - other.GetHashCode();
        }

        public int CompareTo(object obj)
        {
            return this.CompareTo((Card)obj);
        }
    }
}
